package com.gzyj.jtt808.v2019.protocol.yuebiao;


import com.gzyj.jtt808.utils.HexBytesUtil;
import io.netty.buffer.ByteBuf;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lhj
 */
@Data
@ApiModel("报警标识号")
public class AlarmIdentificationProperty implements Serializable {
    @ApiModelProperty("终端ID")
    public String TerminalID;
    /// <summary>
    /// YY-MM-DD-hh-mm-ss
    /// BCD[6]
    /// </summary>
    @ApiModelProperty("时间")
    public Date Time;

    @ApiModelProperty("序号")
    public Byte SN;

    @ApiModelProperty("附件数量")
    public Byte AttachCount;

    @ApiModelProperty("预留")
    public byte[] Retain;

    public void encode(ByteBuf buf) {
        buf.writeBytes(HexBytesUtil.WriteStringPadRight(this.TerminalID,30));
        buf.writeBytes(HexBytesUtil.WriteDateTime_yyMMddHHmmss(this.Time));
        buf.writeByte(this.SN);
        buf.writeByte(this.AttachCount);
        buf.writeBytes(this.Retain);
    }

    public AlarmIdentificationProperty decode(ByteBuf buf) {
        this.TerminalID=HexBytesUtil.readString(buf,30).trim();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            this.Time=sdf.parse(HexBytesUtil.ReadDateTime_yyMMddHHmmss(HexBytesUtil.readByOffset(buf,6)));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.SN=buf.readByte();
        this.AttachCount=buf.readByte();
        this.Retain=new byte[2];
        buf.readBytes(this.Retain);
        return this;
    }


}
